package com.example.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.R;
import com.example.db.DBHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Register extends AppCompatActivity {
    private EditText et_user,et_pwd1,et_pwd2;
    private Button btn_register;
    private DBHelper db;
    private SQLiteDatabase sd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initView();
        getDataBase();
        initEvent();
    }

    private void getDataBase() {
        db = new DBHelper(this);
    }

    private void initEvent() {
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_user.getText().toString().trim();
                String password = et_pwd1.getText().toString().trim();
                String password2 = et_pwd2.getText().toString().trim();
                String str_username = stringFilter(username);
                String str_password = stringFilter(password);
                String str_password2 = stringFilter(password2);
                if(username==null||username.length()==0||password==null||password.length()==0) {
                    Toast.makeText(getApplication(), "账号或密码不能为空！", Toast.LENGTH_LONG).show();
                }else if (!username.equals(str_username)||!password.equals(str_password)) {
                    Toast.makeText(getApplication(),"账号或密码含有特殊字符！",Toast.LENGTH_SHORT).show();
                }else if(!password.equals(password2)){
                    Toast.makeText(getApplication(), "两次密码不同，请重新输入！", Toast.LENGTH_LONG).show();
                }
                else{
                    sd = db.getWritableDatabase();
                    Cursor cursor = sd.query("student",null,"username=?",
                            new String[] {username},null,null,null);

                    if(cursor.getCount() == 0){
                        ContentValues contentValues = new ContentValues();
                        contentValues.put("username",username);
                        contentValues.put("password",password);
                        sd.insert("student",null,contentValues);
                        contentValues.clear();
                        Toast.makeText(getApplication(),"注册成功！",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(getApplication(),Login.class);
                        startActivity(intent);
                        finish();
                    }else{
                        //用户存在清空文本框
                        et_user.setText("");
                        et_pwd1.setText("");
                        et_pwd2.setText("");
                        Toast.makeText(getApplication(), "用户名已存在，请重新输入！", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private void initView() {
        et_user=findViewById(R.id.et_register_user);
        et_pwd1=findViewById(R.id.et_register_pwd1);
        et_pwd2=findViewById(R.id.et_register_pwd2);
        btn_register=findViewById(R.id.btn_register);
    }

    //判断账号或密码为数字或字母
    public static String stringFilter (String str)throws PatternSyntaxException {
        String regEx = "[^a-zA-Z0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}

